<?php
namespace Admin\Controller;
use Think\Controller;
/**
 *用户管理
 */
class UserController extends BaseController {

    /**
     * 用户列表
    */
    public function index(){
        $data=M('admin')->where('type=1')->select();
        $prolist=M('project')->field('proid,pro_name')->select();
        foreach ($data as $k=>$v){
            if($v['forbid']==1){
                $data[$k]['forbid_desc']='已禁用';
            }else{
                $data[$k]['forbid_desc']='正常';
            }
            if($v['edit_permission']==1){
                $data[$k]['edit_permission_desc']='可编辑';
            }else{
                $data[$k]['edit_permission_desc']='仅查看';
            }
            $data[$k]['proname']=$this->getProName($prolist,$v['proid']);
        }
        $this->assign('prolist',$prolist);
        $this->assign('data',$data);
        $this->display();
    }
    /**
     * 删除用户
    */
    public function delUser(){
        $id=$this->_param('id','id缺失');
        $ret=M('admin')->where(['id'=>$id])->delete();
        if($ret){
            $this->_success('操作成功');
        }
        $this->_internalError('操作失败');
    }

    /**
     * 根据id获取项目名
    */
    private function getProName($prolist,$proid){
        if(empty($proid)){
            return '不限';
        }
        if(empty($prolist)){
            return '不限';
        }
        $idarr=explode(',',$proid);
        $proarr=array_column($prolist,'pro_name','proid');
        $str='';
        foreach ($idarr as $id){
            $str.=$proarr[$id].'、';
        }
        return rtrim($str,'、');
    }

    /**
     * 保存用户信息添加或修改
    */
    public function saveEdit(){
        $id=I('id');
        $data=[
            'username'=>$this->_param('username','请填写用户名'),
            'remark'=>$this->_param('remark','请填写姓名及备注'),
            'forbid'=>I('forbid',0),
            'edit_permission'=>I('edit_permission',0),
            'proid'=>I('proid','')
        ];

        if(empty($id)){
            //添加
            $data['type']=1;
            $data['create_time']=date('Y-m-d H:i:s');
            $data['password']=encStr('123456');
            $ret=M('admin')->add($data);
        }else{
            //修改
            $ret=M('admin')->where(['id'=>$id])->save($data);
        }
        if($ret){
            $this->_success('操作成功');
        }
        $this->_internalError('操作失败');
    }

    /**
     * 修改用户密码
    */
    public function modifyPwd(){
        $oldpwd=$this->_param('oldpwd','请输入旧密码');
        $newpwd=$this->_param('newpwd','请输入新密码');
        $newpwd1=$this->_param('newpwd1','请确认新密码');
        if(encStr($oldpwd)!=$this->admin_user['password']){
            $this->_internalError('旧密码错误');
        }
        if($newpwd!=$newpwd1){
            $this->_internalError('两次输入密码不一致');
        }

        $ret=M('admin')->where(['id'=>$this->admin_user['id']])->setField('password',encStr($newpwd));
        if($ret){
            $this->_success('操作成功');
        }
        $this->_internalError('操作失败');
    }



}

